package com.ssm.sfzy.controller;

import com.ssm.sfzy.entity.Good;
import com.ssm.sfzy.entity.TeacherGoods;
import com.ssm.sfzy.service.GoodService;
import com.ssm.sfzy.service.TeacherGoodsService;
import com.ssm.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @ClassName TeacherGoodsController
 * @Author xiaoliu
 * @Date 2024年04月07日 9:07
 */
@RestController
@CrossOrigin
@RequestMapping("/teacherGoods")
public class TeacherGoodsController {

    @Autowired
    private TeacherGoodsService teacherGoodsService;
    @Autowired
    private GoodService goodService;

    @GetMapping("/findById/{id}")
    public Object findById(@PathVariable Integer id){

        List<TeacherGoods> teacherGoods = teacherGoodsService.findAllGoods(id);
        for (TeacherGoods teacherGood : teacherGoods) {
            Good goodName = goodService.findById(teacherGood.getGoodId());
            teacherGood.setGoodName(goodName.getName());
        }
        return Result.success(teacherGoods);
    }

    @PostMapping("/add")
    public Object add(@RequestBody TeacherGoods teacherGoods){
        System.out.println(teacherGoods);

        teacherGoodsService.add(teacherGoods);
        return Result.success();
    }

    @DeleteMapping("/remove/{id}")
    public Object remove(@PathVariable Integer id){
        teacherGoodsService.remove(id);
        return Result.success();
    }
    @GetMapping("/getByTeacherId/{teacherId}")
    public Object getByTeacherId(@PathVariable Integer teacherId){
        List<TeacherGoods> teacherGoodsList = teacherGoodsService.getByTeacherId(teacherId);
        return teacherGoodsList;
    }

}
